/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on 15.08.2007
 * @author Asia Grzibovska
 *
 */

package uk.ac.sanger.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

import uk.ac.sanger.DataManager;
import uk.ac.sanger.WritebackXMLParser;

/**
 * Servlet implementation class for Servlet: AsjaTestServlet
 *
 */
public class WritebackGroups extends HttpServlet implements Servlet {
	// Instantiate this class to provide handlers for the parser and 
	// tell the parser about the handlers
	WritebackXMLParser handler = new WritebackXMLParser();

	public void init(ServletConfig config)
	throws ServletException
	{
		super.init(config);
	}

	//	This is the all-important doPost() method.
	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {	
			HttpSession session = request.getSession(true);
			if (session!=null){

				//first check open id
				if((String)session.getAttribute("openid")==null) {
					String message	 = "you are not logged in, please log in first!";
			    	session.setAttribute("message", message);
		    		response.sendRedirect("login.jsp");
		    		return;
		    	}
				String system_uri = (String)session.getAttribute("system_uri");

				List new_group= new ArrayList();
				new_group.add(request.getParameter("groupid"));
				new_group.add(request.getParameter("attributes"));
				//save data in db
				DataManager dbM = new DataManager();
				dbM.saveGroups(new_group, system_uri);

				String groups = "";
				groups=(String)session.getAttribute("groups");

				if(groups.equals(""))
					groups=new_group.get(0).toString();
				else
					groups=groups+";"+new_group.get(0).toString();

				session.setAttribute("groups",groups);
				//send redirect to feature upload page
				response.sendRedirect("writeback.jsp?arg=NA&missing=none&uri="+system_uri);

			}}
		catch (Exception err){
			err.printStackTrace(); }			
	}

	public void doGet(HttpServletRequest request,
			HttpServletResponse response)
	throws IOException, ServletException
	{
		try
		{
			response.setContentType("text/html");
			PrintWriter out = response.getWriter( );
			out.println ("<html><body>WritebackGroups</body></html>");
			out.println("<HTML><HEAD><TITLE>Servlet</TITLE></HEAD></HTML>");
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage());
		}
	}
}

